package com.example.flashsales.service.impl;


import com.example.flashsales.entity.dto.StoreDto;
import com.example.flashsales.entity.pojo.Store;
import com.example.flashsales.entity.vo.*;
import com.example.flashsales.mappers.StoreMapper;
import com.example.flashsales.service.StoreCostRecordService;
import com.example.flashsales.service.StoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    StoreCostRecordServiceImpl storeCostRecordServiceImpl;

    @Autowired
    StoreMapper storeMapper;

    @Autowired
    GoodsServiceImpl goodsService;

    @Autowired
    OrderServiceImpl orderService;

    @Autowired
    UserRecordServiceImpl userRecordService;

    @Autowired
    UserServiceImpl userService;


    /**
     * 商家增加拉人数
     *
     * @param storeId 商家id
     * @param count   增加的数量
     * @return 返回是否添加成功
     */
    public boolean addUserCount(long storeId, int count) {
        final Store store = storeMapper.viewStoreMessage(storeId);
        if (store == null){
            return false;
        }
        return storeMapper.addUserCount(storeId, count + store.getUserCount()) > 0;
    }


    /**
     * 查看自己的信息
     *
     * @param storeId 店家id
     * @return 返回店家的视图对象
     */
    @Override
    public StoreVo viewStoreMessage(long storeId) {
        final Store store = storeMapper.viewStoreMessage(storeId);
        if (store == null){
            return null;
        }
        StoreVo storeVo = new StoreVo();
        BeanUtils.copyProperties(store,storeVo);
        return storeVo;
    }

    /**
     * 根据电话号码查询商家信息
     * @param phone 电话号码
     * @return 返回商家视图对象
     */
    @Override
    public StoreVo viewMessageByPhone(String phone) {
        StoreVo storeVo = new StoreVo();
        BeanUtils.copyProperties(storeMapper.viewMtssageByPhone(phone),storeVo);
        return storeVo;
    }


    /**
     * 注册店家
     *
//     * @param name     店家名称
//     * @param address  地址
//     * @param account  账号
//     * @param password 密码
//     * @param phone    电话号码
     * @return 返回店家信息
     */
    @Override
    public String register(StoreDto storeDto) {
        final Store store = storeMapper.viewMtssageByPhone(storeDto.getPhone());
        if (store != null){
            return "店家已存在";
        }
        if (storeMapper.register(storeDto) > 0) {
            return "注册成功";
        }
        return "注册失败";
    }


    /**
     * 通过账号密码登录
     *
     * @param phone  账号
     * @param password 密码
     * @return 返回店家信息
     */
    @Override
    public StoreVo loginByAccountAndPassword(String phone, String password) {
        final Store store = storeMapper.viewMtssageByPhone(phone);
        if (store == null || password == "" || !password.equals(store.getPassword())){
            return null;
        }
        return viewMessageByPhone(phone);
    }

    /**
     * 通过电话号码登录
     *
     * @param phone 电话号码
     * @return 返回商家信息
     */
    @Override
    public StoreVo loginByPhone(String phone) {
        return viewMessageByPhone(phone);
    }


    /**
     * 通过旧密码修改密码
     *
     * @param newPassword 新密码
     * @param oldPassword 旧密码
     * @param storeId     店家id
     * @return 返回是否修改成功
     */
    @Override
    public String modifyPasswordByOldPassword(String newPassword, String oldPassword, long storeId) {
        final StoreVo storeVo = viewStoreMessage(storeId);
        final Store store = storeMapper.viewStoreMessage(storeId);
        if (oldPassword == "" || !oldPassword.equals(store.getPassword())){
            return "原密码错误";
        }
        if (storeMapper.updatePassword(storeId,newPassword) > 0) {
            return "修改成功";
        }
        return "修改失败";
    }


    /**
     * 通过短信验证码修改密码
     *
     * @param newPassword 新密码
     * @param storeId     店家id
     * @return 返回是否修改成功
     */
    @Override
    public String modifyPasswordByPhone(String newPassword, long storeId) {
        if (storeMapper.updatePassword(storeId,newPassword) > 0) {
            return "修改成功";
        }
        return "修改失败";
    }


    /**
     * 修改店家地址
     *
     * @param address 地址
     * @param storeId 店家id
     * @return 返回是否修改成功
     */
    @Override
    public boolean modifyAddress(String address, long storeId) {
        final StoreVo storeVo = viewStoreMessage(storeId);
        if (storeVo != null){
            if (storeMapper.modifyAddress(address,storeId) > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     *  查看自己的所有流水纪录
     * @param storeId 店家id
     * @param page 页数
     * @param num 条数
     * @return 返回流水视图对象
     */
    @Override
    public List<StoreCostRecordVo> viewAllStoreCostRecord(long storeId, int page, int num) {
        return storeCostRecordServiceImpl.viewAllStoreCostRecord(storeId,page,num);
    }

    /**
     * 根据时间和类型查看流水纪录
     * @param storeId 店家id
     * @param qDate 前一天
     * @param hDate 后一条
     * @param page 页数
     * @param num 条数
     * @param state 状态
     * @return 返回流水视图对象
     */
    @Override
    public List<StoreCostRecordVo> viewStoreRecordByTimeAndState(long storeId, Date qDate, Date hDate, int page, int num, int state) {
        return storeCostRecordServiceImpl.viewStoreRecordByTimeAndState(storeId,qDate,hDate,page,num,state);
    }


    /**
     * 查看自己的余额
     *
     * @param storeId 店家id
     * @return 返回金额
     */
    @Override
    public Map<String,Double> viewMoney(long storeId) {
        final StoreVo storeVo = viewStoreMessage(storeId);
        Map<String,Double> map = new HashMap<>();
        if (storeVo == null){
            return null;
        }
        map.put("money",storeVo.getMoney());
        return map;
    }

    /**
     * 修改订单状态(用户签收)
     *
     * @param orderId 订单id
//     * @param state   状态 状态 0送达小店 1未送达小店  2已收货 3已分拣 4已下单未支付 5确任收货 6已付款未分拣
     * @return 返回是否修改成功
     */
    @Override
    public boolean updateOrderState(long orderId,long storeId) {
        //修改店家余额
        final StoreVo storeVo = viewStoreMessage(storeId);
        if (orderService.updateOrderState(orderId,2)) {
            if (addOrderStore(storeId,1)) {
                return updateMoney(storeId, storeVo.getOrderMoney(), 2);
            }
        }
        return false;
    }

    /**
     * 增加店家的订单总数
     * @param storeId 店家第
     * @param count 数量
     * @return 返回是否添加成功
     */
    @Override
    public boolean addOrderStore(long storeId, int count) {
        final int orderCount = viewStoreMessage(storeId).getOrderCount();
        return storeMapper.addOrderStore(storeId,count+orderCount) > 0;
    }

    /**
     * 查看商品信息
     *
     * @param goodsId 商品id
     * @return 返回商品视图对象
     */
    @Override
    public GoodsVo viewGoods(long goodsId) {
        return goodsService.viewGoodsMessage(goodsId);
    }


    /**
     * 查看自己的所有订单
     *
     * @param storeId 店家id
     * @return 返回订单视图对象集合
     */
    @Override
    public List<OrdersVo> viewAllOrders(long storeId,int page,int num) {
        final StoreVo storeVo = viewStoreMessage(storeId);
        if (storeVo != null){
            return orderService.viewStoreOrder(storeId,page,num);
        }
        return null;
    }

    /**
     * 查看某个时间段的订单
     *
     * @param storeId   店家id

     * @return 返回订单视图对象集合
     */
    @Override
    public List<OrdersVo> viewOrdersByTime(long storeId, int page, int num, Date qDate, Date hDate) {
        final StoreVo storeVo = viewStoreMessage(storeId);
        if (storeVo != null){
            return orderService.viewStoreOrderByTime(storeId,page,num,qDate,hDate);
        }
        return null;
    }


    /**
     * 查看自己的邀请码
     *
     * @param storeId 店家id
     * @return 返回邀请码
     */
    @Override
    public String viewInviteCode(long storeId) {
        final StoreVo storeVo = viewStoreMessage(storeId);
        if (storeVo != null){
            return storeVo.getInviteCode();
        }
        return null;
    }


    /**
     * 提现
     * @param storeId 店家id
     * @param money 金额
     * @return
     */
    @Override
    public boolean withDro(long storeId, double money) {
        final Map<String, Double> map = viewMoney(storeId);
        if (map == null){
            return false;
        }
        double v = map.get("money");
        if (v < money || money >= 0) {
            return false;
        }

        return updateMoney(storeId, money, 0);
    }

    /**
     * 查看自己拉的用户
     *
     * @param storeId 店家id
     * @return 返回用户视图对象集合
     */
    @Override
    public List<UserVo> viewUsers(long storeId) {
        final List<Integer> list = userRecordService.viewUserIdByStore(storeId);
        List<UserVo> userVos = new ArrayList<>();
        for (Integer integer : list) {
            UserVo userVo = userService.viewUserMessage(integer);
            userVos.add(userVo);
        }
        return userVos;
    }

    /**
     * 修改自己的余额（同时应该在店家流水纪录添加一个纪录）
     *
     * @param storeId 店家id
     * @param money   金额
     * @return 返回是否修改成功
     */
    @Override
    @Transactional
    public boolean updateMoney(long storeId, double money, int state) {
        if (storeCostRecordServiceImpl.addCostRecord(storeId, money, state)) {
            final Map<String, Double> map = viewMoney(storeId);
            if (map == null){
                return false;
            }
            double v = map.get("money");
            money = money + v;
            if (storeMapper.updateMoney(storeId,money) > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 修改店家拉一个的金额
     * @param storeId 店家id
     * @param money 金额
     * @return 返回是否修改成功
     */
    @Override
    public boolean updateStoreUserMoney(long storeId, double money) {
        return storeMapper.updateStoreUserMoney(storeId, money) > 0;
    }

    /**
     * 修改店家成交一单的金额
     * @param storeId 店家id
     * @param money 金额
     * @return 返回是否修改成功
     */
    @Override
    public boolean updateStoreOrderMoney(long storeId, double money) {
        return storeMapper.updateStoreOrderMoney(storeId, money) > 0;
    }

    /**
     * 修改店家状态
     * @param storeId 店家第
     * @param state 状态
     * @return 返回是否修改成功
     */
    @Override
    public boolean updateStoreState(long storeId, int state) {
        return storeMapper.updateStoreState(storeId,state) > 0;
    }

    /**
     * 根据邀请码获取店家信息
     * @param inviteCode
     * @return
     */
    @Override
    public Store viewStoreMessageByInviteCode(String inviteCode) {
        return storeMapper.viewStoreMessageByInviteCode(inviteCode);
    }

    /**
     * 查看流水纪录条数
     * @param storeId
     * @return
     */
    @Override
    public Integer viewCostRecordCount(Long storeId) {
        return storeMapper.viewCostRecordCount(storeId);
    }
}
